#!/usr/bin/env python 3.6
# -*- coding: utf-8 -*-
# @Time      :2021/6/21
# @Author    :zhangxuchuo
# @Email     :zhangxuchuo@bbdservice.com
# @File      :Rule14_EntEnviDefaultLevel
# @Software  :PyCharm


'''eof
name:申请企业环保失信等级校验（五年内）
code:Rule14_EntEnviDefaultLevel
tableName:
columnName:
groups:企业通用模块
dependencies:FGW_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import json
import re
import sys
import time,datetime
import pandas as pd
import math
from dateutil import rrule
from dateutil.relativedelta import relativedelta
import io

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def Rule14_EntEnviDefaultLevel():
    try:
        FGW = FGW_DSJ.get('data')
        Levelinfo = FGW.get('stClCreditLevelInfoD', '')
        if Levelinfo == '':
            return '缺失值'
        else:
            time = []
            grade = []
            for i in Levelinfo:
                if i.get('assessTime', '') == '':
                    res = i.get('evrmCreditGrade', '')
                    if res not in null_type_list:
                        if '环保领域严重失信生产者' in res:
                            return 1
                        else:
                            return 0
                    else:
                        return '缺失值'
                else:
                    time.append(int(i.get('assessTime', '')))
                    grade.append(int(i.get('evrmCreditGrade', '')))
        maxTime = max(time)
        index = grade.index(maxTime)
        res = grade[index]
        if res not in null_type_list:
            if '环保领域严重失信生产者' in res:
                return 1
            else:
                return 0
        else:
            return '缺失值'
    except:
        return '缺失值'


if __name__ == '__main__':
    import json
    df = pd.read_csv(r'C:\Users\zdr\Desktop\samples\FGW.csv', engine='python', error_bad_lines=False)
    print(df)
    data = json.loads(df['data'][1])['data']
result = Rule14_EntEnviDefaultLevel()